#!/bin/bash
# build script for building rtsb to rmp package
#
#define variable
PACKAGE_NAME=rtsdb
PACKAGE_VERSION=3.2.9
WORK_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
BUILD_DIR=${WORK_DIR}/build
RPM_BUILD_DIR=${BUILD_DIR}/rpmbuild

pushd ${WORK_DIR}
# make build script runnable
find . -name "*.sh" | xargs -n 1 sudo chmod +x
find . -name "configure" | xargs -n 1 sudo chmod +x
sudo chmod +x runtest
sudo chmod +x runtest-cluster
sudo chmod +x runtest-sentinel

echo begin init build env
rm -rf ${BUILD_DIR}

mkdir -p ${BUILD_DIR}
mkdir -p ${BUILD_DIR}/${PACKAGE_NAME}-${PACKAGE_VERSION}/
mkdir -p ${RPM_BUILD_DIR}/{BUILD,RPMS,SOURCES,SRPMS}
# create tar file
declare -a filelist
filelist=("Makefile")
filelist+=("redis.conf")
filelist+=("sentinel.conf")
filelist+=("runtest")
filelist+=("runtest-cluster")
filelist+=("runtest-sentinel")
filelist+=("deps/")
filelist+=("src/")
filelist+=("tests/")
filelist+=("utils/")
for x in "${filelist[@]}"
do
  cp -R ${x} ${BUILD_DIR}/${PACKAGE_NAME}-${PACKAGE_VERSION}/
done

pushd ${BUILD_DIR}
rm -f  ${PACKAGE_NAME}-${PACKAGE_VERSION}.tar.gz
tar czvf ${PACKAGE_NAME}-${PACKAGE_VERSION}.tar.gz  ${PACKAGE_NAME}-${PACKAGE_VERSION}/

# create build resource
mv ${PACKAGE_NAME}-${PACKAGE_VERSION}.tar.gz ${RPM_BUILD_DIR}/SOURCES/
cp ${WORK_DIR}/redis.conf ${RPM_BUILD_DIR}/SOURCES/
cp ${WORK_DIR}/utils/redis_init_script ${RPM_BUILD_DIR}/SOURCES/rtsdb

#rpm build
rpmbuild --define "_topdir ${RPM_BUILD_DIR}" --define "debug_package %{nil}" -ba  ${WORK_DIR}/rpm/redis.spec

popd
popd
